package com.xnew.common.utils;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName EncryUtils
 * @Description TODO
 * @Author tinx
 * @Date 2019/6/27 0027 下午 3:08
 * @Version 1.0
 **/
public class EncryUtils {

    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "a", "b", "c", "d", "e", "f"};

    private final static String BASE_ENCRY_STR = "ThIsisUMInifIerXshOpSsm_BaSEeNcrY#";

    public static String currDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        String time = sdf.format(date);
        return time;


    }

    public static String preDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -1);// 3分钟之前的时间
        Date beforeD = beforeTime.getTime();
        String time = sdf.format(beforeD);
        return time;

    }

    public static String getEncryStr(){
        String dateTime = currDateTime();
        System.out.println(dateTime);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < dateTime.length(); i++){
            sb.append(dateTime.charAt(i)).append(BASE_ENCRY_STR.charAt(i));
        }
        System.out.println(sb.toString());
        return MD5Encode(sb.toString());
    }

    public static String getPreEncryStr(){
        String dateTime = preDateTime();
        System.out.println(dateTime);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < dateTime.length(); i++){
            sb.append(dateTime.charAt(i)).append(BASE_ENCRY_STR.charAt(i));
        }
        System.out.println(sb.toString());
        return MD5Encode(sb.toString());
    }

    /**
     * 转换字节数组为16进制字串
     *
     * @param b 字节数组
     * @return 16进制字串
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuilder resultSb = new StringBuilder();
        for (byte aB : b) {
            resultSb.append(byteToHexString(aB));
        }
        return resultSb.toString();
    }

    /**
     * 转换byte到16进制
     *
     * @param b 要转换的byte
     * @return 16进制格式
     */
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static String MD5Encode(String origin) {
        String resultString = null;
        try {
            resultString = origin;
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(resultString.getBytes("UTF-8"));
            resultString = byteArrayToHexString(md.digest());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    public static void main(String[] args){
        System.out.println(getEncryStr());
    }
}